/**
 * Created by dh.huang on 2016/8/30.
 */

angular.module('inspinia')
  .controller('modelingCtrl', function ($scope,modelList,SweetAlert,$interval) {

    $scope.modelList = modelList.modelList;
    $scope.noCheckList = [];
    $scope.modelForm = {};
    $scope._patients = modelList.patients;
    $scope.search = {};
    $scope.length = false;
    $scope.curentId = -1;
    var _index = -1;

    //监听在此时间段之前没有压模的病人
    var _watch = function(){
      var _array = [];
      angular.forEach($scope.modelList,function(data,index,array){
        var _times = data.time.split(':');
        var _now = new Date();
        var _nowHours = _now.getHours();
        var _nowMin = _now.getMinutes();

        if(_times[0] < _nowHours || (_times[0] == _nowHours && _times[1]<_nowMin-30)){
          for(var i=0;i<data.patients.length;i++){
            if($scope.length){
              for(var j=0;j<$scope.noCheckList.length;j++){
                if($scope.noCheckList[j].id == data.patients[i].id){
                  break;
                }
              }
              var obj =data.patients[i];
              _array.push(obj);
            }else{
              var obj =data.patients[i];
              _array.push(obj);
              $scope.length = true;
            }
          }
        }
      });
      $scope.noCheckList = _array;
      if($scope.length){
        $("#noCheck").show();
      }
    };
    var _timer = function(){
      $interval(function(){
        _watch();
      },1000);
    };

    $scope.init = function(){
      $('#search-content').slideToggle(10);
      $('#pm-content').slideToggle(10);
      var icon = $('#ibox-search').find('i:first');
      icon.toggleClass('fa-chevron-up').toggleClass('fa-chevron-down');
      $('#finish').attr('disabled',true);
     // _watch();
      //_timer();
    };

    $scope.init();


      //图片点击事件，显示处方笺
    $scope.showModelForm = function(id){
      $scope.curentId = id;
      $scope.modelForm = null;
      $('#finish').attr('disabled',false);
      angular.forEach($scope.modelList,function(data,index,array) {
        for (var i = 0; i < data.patients.length; i++) {
          if (data.patients[i].id == id) {
            $scope.modelForm = data.patients[i];
          }
        }
      });
      console.log($scope.modelForm);
      /*for(var i =0;i<$scope._patients.length;i++){
        if($scope._patients[i].id == id){
          $scope.modelForm = $scope._patients[i];

          _index = i;
        }
      }*/
    };

    //完成
    $scope.finish = function(id){

      swal({
          title: "压模",
          text: "压模操作是否已完成",
          type: "warning",
          showCancelButton: true,
          confirmButtonColor: "#DD6B55",
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          closeOnConfirm: true,
          closeOnCancel: true
        },
        function(isConfirm){
            if (isConfirm) {
              $scope._patients[_index] = $scope.modelForm;
              // $scope._patients[_index].status = '已完成';
              angular.forEach($scope.modelList,function(data,index,array) {
                for (var i = 0; i < data.patients.length; i++) {

                  if (data.patients[i].id == $scope.curentId) {
                    $scope.modelList[index].patients[i].status = '已完成';
                    $('#finish').attr('disabled',true);
                  }
                }
              });
              $scope.$apply();
              swal("确定", "已完成", "success");
            }else{
              swal("取消", "", "error");
            }
        });
    };
  })
